<?php
	include("../service/receiptService.php");
	include("../service/receiptItemService.php");
	include("../service/ProductService.php");
	include("../util/smtp.php");
	
	session_start();
	
	$receiver = $_POST['receiver'];
	$phone = $_POST['phone'];
	$address = $_POST['email'];
	$note = $_POST['note'];
	$userId = $_SESSION['username'][0];
	$cart = $_SESSION['cart'];
	
	$code = getReceiptCode($userId);
	
 	addReceipt($userId, $receiver, $address, $phone, $note, $code,1);
 	//$receiptId = ConnectionManager::getInstance()->getLastId();
	$receiptId = getLatestRecieptId();
	// Add receipt item for each product
 	foreach ($cart as $proId => $proQuan){
		$product = mysql_fetch_array(getProduct($proId));
		$cost = $proQuan * $product['PRO_PRICE'];
		addReceiptItem($proId, $receiptId, $proQuan, $cost);
	}
	
	// Send mail
	$mailMessage = "Người nhận: $receiver<br/>
				   SĐT: $phone<br/>
				   Email: $address<br/>
				   Tin nhắn: $note
				   Mã đơn hàng: $code;";
	
	SendMail("info@giaan4s.com","info@giaan4s.com", "Đặt Hàng Gia An", $mailMessage, $_SESSION['username'][1]);
	
	echo "<script>window.location.href='../pc/order.php?msg=ok';</script>";
?>